package com.infomanage.service.study;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.jsp.JspWriter;

import com.infomanage.authority.AuthorityBean;
import com.infomanage.bean.FieldBean;
import com.infomanage.bean.PaginationBean;
import com.infomanage.bean.SearchBean;
import com.infomanage.dao.PersonDAO;
import com.infomanage.dao.StudyRescueDAO;
import com.infomanage.dao.StudyTechBusinessDAO;
import com.infomanage.enums.RoleType;
import com.infomanage.po.Person;
import com.infomanage.po.StudyTechBusiness;
import com.infomanage.po.User;
import com.infomanage.util.Consts;
import com.infomanage.util.StringUtil;
import com.infomanage.vo.StudyTechBusinessItem;
import com.jiuqi.util.ReturnObject;
import com.jiuqi.util.StringUtils;

public class StudyTechBusinessService {

	public static StudyTechBusinessItem loadStudyTechBusiness(HttpServletRequest request, HttpServletResponse response,
			JspWriter out) {
		try {
			request.setCharacterEncoding("UTF-8");
			String itemId = request.getParameter("itemId");
			StudyTechBusinessDAO dao = new StudyTechBusinessDAO();
			if (StringUtils.isEmpty(itemId))
				return null;
			return dao.find(Integer.parseInt(itemId));
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	public static List<StudyTechBusinessItem> loadStudyTechBusinessList(HttpServletRequest request,
			HttpServletResponse response, JspWriter out) {
		try {
			request.setCharacterEncoding("UTF-8");
			StudyTechBusinessDAO dao = new StudyTechBusinessDAO();
			List<StudyTechBusinessItem> list = dao.findAll();
			return list;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	public static ReturnObject saveStudyTechBusiness(HttpServletRequest request, HttpServletResponse response,
			JspWriter out) {
		ReturnObject ret = new ReturnObject();
		try {
			request.setCharacterEncoding("UTF-8");
			StudyTechBusiness study = new StudyTechBusiness();
			study.setCommanderId(Integer.parseInt(request.getParameter("commanderId")));
			study.setDescription(request.getParameter("description"));
			study.setDetail(request.getParameter("detail"));
			study.setParticipants(request.getParameter("participants"));
			study.setPlace(request.getParameter("place"));
			study.setProblem(request.getParameter("problem"));
			study.setSubjects(request.getParameter("subjects"));
			study.setTimestamp(request.getParameter("timestamp"));
			PersonDAO personDAO = new PersonDAO();
			Person person = personDAO.find(Person.class, Integer.parseInt(request.getParameter("commanderId")));
			if(person != null){
				study.setDeptId(person.getDeptId());
			}
			StudyTechBusinessDAO dao = new StudyTechBusinessDAO();
			dao.add(study);
		} catch (Exception e) {
			ret.SetErrored(true);
			ret.setErrorMessage(e.getMessage());
		}
		return ret;
	}

	public static ReturnObject removeStudyTechBusiness(HttpServletRequest request, HttpServletResponse response,
			JspWriter out) {
		ReturnObject ret = new ReturnObject();
		try {
			request.setCharacterEncoding("UTF-8");
			String studyId = request.getParameter("itemId");
			StudyTechBusinessDAO dao = new StudyTechBusinessDAO();
			dao.remove(StudyTechBusiness.class, Integer.parseInt(studyId));
		} catch (Exception e) {
			ret.SetErrored(true);
			ret.setErrorMessage(e.getMessage());
		}
		return ret;
	}

	public static ReturnObject removeBatchStudyTechBusiness(HttpServletRequest request, HttpServletResponse response,
			JspWriter out) {
		ReturnObject ret = new ReturnObject();
		try {
			request.setCharacterEncoding("UTF-8");
			String batchIds = request.getParameter("batchIds");
			StudyTechBusinessDAO dao = new StudyTechBusinessDAO();
			if (batchIds != null) {
				String[] ids = batchIds.split(";");
				for (String id : ids) {
					dao.remove(StudyTechBusiness.class, Integer.parseInt(id));
				}
			}
		} catch (Exception e) {
			ret.SetErrored(true);
			ret.setErrorMessage(e.getMessage());
		}
		return ret;
	}

	public static ReturnObject modifyStudyTechBusiness(HttpServletRequest request, HttpServletResponse response,
			JspWriter out) {
		ReturnObject ret = new ReturnObject();
		try {
			request.setCharacterEncoding("UTF-8");
			int planId = Integer.parseInt(request.getParameter("itemId"));
			StudyTechBusinessDAO dao = new StudyTechBusinessDAO();
			StudyTechBusiness study = dao.find(StudyTechBusiness.class, planId);
			study.setCommanderId(Integer.parseInt(request.getParameter("commanderId")));
			study.setDescription(request.getParameter("description"));
			study.setDetail(request.getParameter("detail"));
			study.setParticipants(request.getParameter("participants"));
			study.setPlace(request.getParameter("place"));
			study.setProblem(request.getParameter("problem"));
			study.setSubjects(request.getParameter("subjects"));
			study.setTimestamp(request.getParameter("timestamp"));
			PersonDAO personDAO = new PersonDAO();
			Person person = personDAO.find(Person.class, Integer.parseInt(request.getParameter("commanderId")));
			if(person != null){
				study.setDeptId(person.getDeptId());
			}
			dao.modify(study);
		} catch (Exception e) {
			ret.SetErrored(true);
			ret.setErrorMessage(StringUtil.filterIllegalchar(e.getMessage()));
		}
		return ret;
	}

	public static List<StudyTechBusinessItem> search(HttpServletRequest request, HttpServletResponse response,
			JspWriter out) {
		try {
			request.setCharacterEncoding("UTF-8");
			StudyTechBusinessDAO dao = new StudyTechBusinessDAO();
			SearchBean searchBean = buildSearchCondition(request, response, out);
			// 设置分页
			String currPageNum = request.getParameter("currPageNum");
			PaginationBean page = new PaginationBean();
			if (currPageNum == null) {
				page.setCurrentPageNum(1);
			} else {
				page.setCurrentPageNum(Integer.parseInt(currPageNum));

			}
			searchBean.setPage(page);
			List<StudyTechBusinessItem> list = dao.findByFields(searchBean);
			return list;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	public static int getPageCount(HttpServletRequest request, HttpServletResponse response, JspWriter out) {
		try {
			StudyTechBusinessDAO dao = new StudyTechBusinessDAO();
			SearchBean searchBean = buildSearchCondition(request, response, out);
			return (dao.count(searchBean) + PaginationBean.pageSize - 1) / PaginationBean.pageSize;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return 1;
	}

	private static SearchBean buildSearchCondition(HttpServletRequest request, HttpServletResponse response,
			JspWriter out) throws Exception {
		request.setCharacterEncoding("UTF-8");
		String timestamp = request.getParameter("timestamp");
		String place = request.getParameter("place");
		String organizersId = request.getParameter("organizersId");
		SearchBean searchBean = new SearchBean();
		if (!StringUtil.isEmpty(timestamp)) {
			FieldBean field = new FieldBean("timestamp", timestamp);
			searchBean.add(field);
		}
		if (!StringUtil.isEmpty(place)) {
			FieldBean field = new FieldBean("place", place);
			field.setLike(true);
			searchBean.add(field);
		}
		if (!StringUtil.isEmpty(organizersId)) {
			FieldBean field = new FieldBean("organizersId", organizersId);
			searchBean.add(field);
		}

		// 设置权限
		User user = (User) request.getSession().getAttribute(Consts.LOGINED_USER);
		if (user != null) {
			AuthorityBean authority = new AuthorityBean();
			authority.setRole(RoleType.valueOf(user.getRoleId()));
			authority.setDeptId(user.getAssociatedDept());
			searchBean.setAuthority(authority);
		}
		return searchBean;
	}
}
